package com.jd.controller;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jd.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.entity.NameEntity;
import com.jd.entity.VMyTicketEntity;
import com.jd.service.NameSrevice;
import com.jd.service.TicketService;

/**
 * @author masicheng 
 * 时间 : 2020年12月21日 下午1:44:35 
 * 注释 :门票
 */

@Controller
@RequestMapping("ticketController")
public class TicketController {

	@Autowired
	private TicketService ticketService;
	@Autowired
	private NameSrevice nameSrevice;
	@Autowired
	private IGoodsService goodsService;
	
	//我的门票
	@RequestMapping("/myTickets")
	@ResponseBody
	public ArrayList<VMyTicketEntity> myTickets(HttpServletRequest request){
		String userId = request.getSession().getAttribute("userId").toString();
		ArrayList<VMyTicketEntity> myTicketsList= ticketService.getMyTickets(userId);
		return myTicketsList;
	}
	
	@RequestMapping("/payTickets")
	@ResponseBody
	public ArrayList<ArrayList<NameEntity>> payTickets(HttpServletRequest request,Integer type){
		ArrayList<ArrayList<NameEntity>> all = new ArrayList<ArrayList<NameEntity>>();

		ArrayList<NameEntity> list = new ArrayList<NameEntity>();

		ArrayList<NameEntity> list1 = new ArrayList<NameEntity>();
		ArrayList<NameEntity> list2 = new ArrayList<NameEntity>();

		if(type == 1){
			ArrayList<Map<String, String>> gameList = nameSrevice.getGameList("0");
			for (Map<String, String> map : gameList) {
				String id = map.get("id");
				ArrayList<NameEntity> myTicketsList= ticketService.payTickets(id);
				for (NameEntity nameEntity : myTicketsList) {
					nameEntity.setType(1);
					list.add(nameEntity);
				}
			}
		}else{
			//商品
			ArrayList<NameEntity> listAll = goodsService.getListAll();
			for (NameEntity nameEntity : listAll) {
				nameEntity.setType(0);
				list.add(nameEntity);
			}
		}

		for (int i = 0; i < list.size(); i++) {
			if(i%2==0){
				list2.add(list.get(i));
			}else{
				list1.add(list.get(i));
			}
		}

		all.add(list1);
		all.add(list2);
		return all;
	}
}
